package cn.edu.sdjzu.xg.kcsj.controller.authority;

import cn.edu.sdjzu.xg.kcsj.security.ResourceAssRole;
import cn.edu.sdjzu.xg.kcsj.security.ResourceAssRoleService;
import cn.edu.sdjzu.xg.kcsj.util.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/resourceAssRoleBatchInsert")
public class ResourceAssRoleBatchInsertController extends HttpServlet {
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    JSONObject message = new JSONObject();
    String json = JSONUtil.getJSON(request);
    PrintWriter writer = response.getWriter();
    
    List<ResourceAssRole> resourceAssRoles = JSON.parseArray(json, ResourceAssRole.class);
    try {
      ResourceAssRoleService.getInstance().batchInsert(resourceAssRoles);
      message.put("message", "提交成功");
      writer.println(message);
    } catch (Exception e) {
      message.put("message", "提交失败");
      //响应message到前端
      writer.println(message);
      // 在命令行打印异常信息在程序中出错的位置及原因
      e.printStackTrace();
    }
  }
}
